package com.jcl.elasticsearch.core.utils;

import com.jcl.core.common.page.PageResult;
import com.jcl.core.utils.CollectionUtils;
import com.jcl.core.utils.StringUtils;
import com.jcl.elasticsearch.core.entity.ElasticsearchEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ElasticConfigUtils
 * @Description TODO
 * @Author yyliu
 * @Date 2018/11/1 15:19
 * @Version 1.0
 **/
public class ElasticConfigUtils {

    public static final String        SPLIT_URL                    = "/";
    public static final String        SPLIT_ROOM                   = ",";
    public static final String        SPLIT_GROUP                  = ";";
    public static final String        ES_SCHEME                    = "http";
    public static final String        ES_HITS                      = "hits";

    public static final String        ES_METHOD                    = "POST";
    public static final String        ES_METHOD_GET                = "GET";
    public static final String        ES_METHOD_PUT                = "PUT";
    public static final String        ES_METHOD_DEL                = "DELETE";

    public static final String        ENDPOINT_SEARCH              = "/_search";

    private static Map<String, List<Integer>> host_port = new HashMap<>();
    public static String index = null;
    public static String type = null;

    public static Map<String, List<Integer>> getHostPortMap(){
        return host_port;
    }

    public static String getIndex(){
        return index;
    }

    public static void initHostPort(String hostPort){
        if(StringUtils.hasLength(hostPort)){
            String[] $arr1 = hostPort.split(SPLIT_GROUP);
            for(String str1: $arr1){
                String[] $arr2 = str1.split(SPLIT_ROOM);
                addHostPort($arr2[0], Integer.valueOf($arr2[1]));
            }
        }
    }
    public static void initEndpoint(String index, String type){
        ElasticConfigUtils.index = index;
        ElasticConfigUtils.type = type;
    }

    private static void addHostPort(String host, Integer port){
        List<Integer> list = host_port.get(host);
        if(null == list){
            list = new ArrayList<>();
            ElasticConfigUtils.host_port.put(host, list);
        }
        if(!list.contains(port)){
            list.add(port);
        }
    }

    public static PageResult searchPageResult(ElasticsearchEntity elasticsearchEntity, Integer curPage){
        List<Map<String, Object>> dataList = new ArrayList<>();
        Long total = 0L;
        if(null != elasticsearchEntity){
            total = elasticsearchEntity.getTotal();
            List<ElasticsearchEntity.Hits> entityHits = elasticsearchEntity.getHits();
            if(CollectionUtils.isNotEmpty(entityHits)){
                for(ElasticsearchEntity.Hits entity: entityHits){
                    // 需要过滤字段
                    Map<String, Object> dataMap = entity.get_source();
                    dataMap.remove("@version");
                    dataMap.remove("@timestamp");
                    dataList.add(dataMap);
                }
            }
        }
        PageResult<Map<String, Object>> result = new PageResult(curPage, null, total, dataList);
        return result;
    }
}
